﻿Imports System
Imports System.IO
Imports System.Text
Imports System.IO.Path
Imports System.Drawing.Imaging
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Xml

Public Class ConvertLibrary
    Inherits Form

    Sub SetFormIcon()
        Me.Icon = My.Resources.ConvertLibraryIcon
    End Sub

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click

        If lstMediaLocations.Items.Count < 1 Then
            Dim msgboxee As Integer = MsgBox("Please select a valid folder path.", MsgBoxStyle.OkOnly, "Invalid Folder Path")
            If msgboxee = 1 Then
                Exit Sub
            End If
        End If

        If btnMediaBrowserToXBMC.Checked = True Then
            MediaBrowserToXBMC()
        End If

        If btnXMBCToMediaBrowser.Checked = True Then
            XMBCToMediaBrowser()
        End If

        If chkDone.Checked = True Then
            MsgBox("Done")
        End If

        If chkCloseAfter.Checked = True Then
            CloseWhenComplete()
        End If

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs)
        ' The keypressed method uses the KeyChar property to check 
        ' whether the ENTER key is pressed. 

        ' If the ENTER key is pressed, the Handled property is set to true, 
        ' to indicate the event is handled.

        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            e.Handled = True

            If lstMediaLocations.Items.Count < 1 Then
                Dim msgboxee As Integer = MsgBox("Please select a valid folder path.", MsgBoxStyle.OkOnly, "Invalid Folder Path")
                If msgboxee = 1 Then
                    Exit Sub
                End If
            End If

            If btnMediaBrowserToXBMC.Checked = True Then
                MediaBrowserToXBMC()
            End If

            If btnXMBCToMediaBrowser.Checked = True Then
                XMBCToMediaBrowser()
            End If

            If chkDone.Checked = True Then
                MsgBox("Done")
            End If

            If chkCloseAfter.Checked = True Then
                CloseWhenComplete()
            End If
        End If
    End Sub 'keypressed

    Private Sub CloseWhenComplete()
        'Closes the app.
        Me.Close()
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        lstMediaLocations.Items.Remove(lstMediaLocations.SelectedItem)
    End Sub

    Private Sub btnBrowseMediaLocations_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseMediaLocations.Click
        With fbdLocation1
            .Description = "Select Root Media Folder"
            .RootFolder = Environment.SpecialFolder.MyComputer
            .ShowNewFolderButton = False
        End With

        If fbdLocation1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim SelectedFolder As String = fbdLocation1.SelectedPath
            If lstMediaLocations.Items.Contains(SelectedFolder.ToString) = True Then
            Else
                Me.lstMediaLocations.Items.Add(fbdLocation1.SelectedPath)
            End If
        End If
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        'My.Settings.FolderList.Clear()

        'For Each item As String In lstMediaLocations.Items
        'My.Settings.FolderList.Add(item)
        'Next
    End Sub


    Public Shared Function RemoveAttribute(ByVal attributes As FileAttributes, ByVal attributesToRemove As FileAttributes) As FileAttributes
        'This is for setting the file attribute.
        Return attributes And (Not attributesToRemove)
    End Function

    Private Sub lblLocation1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub btnXMBCToMediaBrowser_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXMBCToMediaBrowser.CheckedChanged

    End Sub

    Private Sub btnMediaBrowserToXBMC_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMediaBrowserToXBMC.CheckedChanged

    End Sub

    Private Sub chkTrailers_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTrailers.CheckedChanged

    End Sub

    Private Sub chkFanart_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFanart.CheckedChanged

    End Sub

    Private Sub MediaBrowserToXBMC()
        For Each item As String In lstMediaLocations.Items()
            Dim Files As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(item, FileIO.SearchOption.SearchAllSubDirectories)
            Dim FileCollection As New Microsoft.VisualBasic.Collection()

            'Progress Bar sets maximum to the number of files returned in the count of "files."
            With pbrBar1
                .Maximum = Files.Count
                .Minimum = 0
                .Value = 1
            End With

            'Processes each file inside of the text box directory.
            For Each file In Files
                Dim strFileExtension As String = System.IO.Path.GetExtension(file)
                Dim strCurrentFile As String = System.IO.Path.GetFileName(file)
                Dim strFilePath As String = System.IO.Path.GetDirectoryName(file)

                'Advances the progress bar one level for each file processed
                If pbrBar1.Value < pbrBar1.Maximum Then
                    pbrBar1.Value = pbrBar1.Value + 1
                End If

                'Checks to see if the file being processed actually exists. If not it skips to the next file. Needed for folders with more than one trailer inside of it.
                If My.Computer.FileSystem.FileExists(file) Then
                    'Checks to see if the file is a system hidden file.  Example: $recycler.
                    If file.Contains("$") Then
                    Else
                        'Checks to see if ReadOnly attribute is set and switches it off.
                        If GetAttr(file) And vbReadOnly Then
                            Dim attributes As FileAttributes

                            attributes = GetAttr(file)

                            If (attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
                                attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly)
                                SetAttr(file, attributes)
                            End If
                        End If

                        'Checks if the fanart checkbox is checked.
                        If chkFanart.Checked = True Then
                            Dim stringName = Convert.ToString(strCurrentFile)

                            'Checks to make sure a file is a picture before renaming and moving it.
                            If strFileExtension.ToLower.Contains("jpg") Then
                                'Checks to see if a file is named backdrop before renaming and moving or deleting it.
                                If strCurrentFile.ToLower.Contains("backdrop") Then
                                    'If the file contains a number for example "backdrop1.jpg" and deletes it.
                                    If strCurrentFile.Length() > 12 Then
                                        If chkDeleteBackdrop.Checked = True Then
                                        Else
                                            'Checks to see if ReadOnly attribute is set and switches it off.
                                            If GetAttr(strFilePath + "\" + strCurrentFile) And vbReadOnly Then
                                                Dim attributes As FileAttributes

                                                attributes = GetAttr(strFilePath + "\" + strCurrentFile)

                                                If (attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
                                                    attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly)
                                                    SetAttr(strFilePath + "\" + strCurrentFile, attributes)
                                                End If
                                            End If
                                            'Deletes backdrop with number in it.
                                            My.Computer.FileSystem.DeleteFile(strFilePath + "\" + strCurrentFile)
                                        End If
                                    Else
                                        'Checks to see if a fanart file already exists and deletes the current file if true.
                                        If My.Computer.FileSystem.FileExists(strFilePath + "\" + "fanart.jpg") Then
                                            'Checks to see if ReadOnly attribute is set and switches it off.
                                            If GetAttr(strFilePath + "\" + "fanart.jpg") And vbReadOnly Then
                                                Dim attributes As FileAttributes

                                                attributes = GetAttr(strFilePath + "\" + "fanart.jpg")

                                                If (attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
                                                    attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly)
                                                    SetAttr(strFilePath + "\" + "fanart.jpg", attributes)
                                                End If
                                            End If
                                            My.Computer.FileSystem.DeleteFile(strFilePath + "\" + "fanart.jpg")
                                            'Renames file to fanart.
                                            My.Computer.FileSystem.MoveFile(file, strFilePath + "\" + "fanart" + strFileExtension)
                                        Else
                                            'If the file survives all the rest it is renamed to fanart.
                                            My.Computer.FileSystem.MoveFile(file, strFilePath + "\" + "fanart" + strFileExtension)
                                        End If
                                    End If
                                End If
                            End If

                            'Checks to see if the file extension contains "png."
                            If strFileExtension.ToLower.Contains("png") Then
                                'Checks to see if the png is a backdrop.
                                If strCurrentFile.ToLower.Contains("backdrop") Then
                                    'If the file contains a number for example "backdrop1.png" and deletes it.
                                    If strCurrentFile.Length() > 12 Then
                                        If chkDeleteBackdrop.Checked = True Then
                                        Else
                                            'Checks to see if ReadOnly attribute is set and switches it off.
                                            If GetAttr(strFilePath + "\" + strCurrentFile) And vbReadOnly Then
                                                Dim attributes As FileAttributes

                                                attributes = GetAttr(strFilePath + "\" + strCurrentFile)

                                                If (attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
                                                    attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly)
                                                    SetAttr(strFilePath + "\" + strCurrentFile, attributes)
                                                End If
                                            End If
                                            My.Computer.FileSystem.DeleteFile(strFilePath + "\" + strCurrentFile)
                                        End If
                                    Else
                                        Dim strFileNameNoExtension As String = System.IO.Path.GetFileNameWithoutExtension(file)
                                        Dim curBitmap As New Bitmap(file)
                                        Dim strFileName As String = System.IO.Path.GetFullPath(file)

                                        If My.Computer.FileSystem.FileExists(strFilePath + "\" + "fanart.jpg") Then
                                            If chkDeleteBackdrop.Checked = True Then
                                            Else
                                                curBitmap.Dispose()
                                                'Deletes png
                                                My.Computer.FileSystem.DeleteFile(file)
                                            End If
                                        Else
                                            'Converts the PNG to JPG.
                                            curBitmap.Save(strFilePath + "\" + "fanart.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
                                            curBitmap.Dispose()
                                            'Deletes png
                                            My.Computer.FileSystem.DeleteFile(file)
                                        End If
                                    End If
                                End If
                            End If
                        End If

                        'Checks to see if the the trailers check box is checked.
                        If chkTrailers.Checked = True Then
                            Dim strFullFilePath As String = System.IO.Path.GetFileNameWithoutExtension(file)
                            Dim Current As String
                            Dim Parent As DirectoryInfo

                            'This is a process to find the parent directory of the current file.
                            Try
                                Directory.SetCurrentDirectory(strFilePath)
                                Current = Directory.GetCurrentDirectory()
                                Parent = Directory.GetParent(Current)
                                Console.WriteLine("Current directory {0}", Current)
                                Console.WriteLine("Parent directory {0}", Parent.FullName)
                                Dim ParentDir As String = Parent.FullName

                                'Checks to see if the current path contains trailers so we know if a trailer is being processed.
                                If strFilePath.ToLower.Contains("trailers") Then
                                    'Checks to see if the trailer is already named correctly.
                                    If file.ToLower.Contains("-trailer") Then
                                        'Checks to see if a file with -trailer already exists.
                                        If My.Computer.FileSystem.FileExists(ParentDir + "\" + strFullFilePath + strFileExtension) Then
                                            If chkDeleteTrailer.Checked = True Then
                                                Dim Counter As Integer
                                                Dim CounterValue

                                                Counter = Counter + 1
                                                CounterValue = Convert.ToString(Counter)

                                                'Moves the file to the parent directory and renames it.
                                                My.Computer.FileSystem.MoveFile(file, ParentDir + "\" + strFullFilePath + CounterValue + strFileExtension)
                                                'Checks if trailers folder is empty
                                                If System.IO.Directory.GetFiles(strFilePath).Count > 0 Then
                                                Else
                                                    My.Computer.FileSystem.DeleteDirectory(strFilePath, FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing)
                                                End If
                                            Else
                                                'Checks to see if ReadOnly attribute is set and switches it off.
                                                If GetAttr(ParentDir + "\" + strFullFilePath + strFileExtension) And vbReadOnly Then
                                                    Dim attributes As FileAttributes

                                                    attributes = GetAttr(ParentDir + "\" + strFullFilePath + strFileExtension)

                                                    If (attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
                                                        attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly)
                                                        SetAttr(ParentDir + "\" + strFullFilePath + strFileExtension, attributes)
                                                    End If
                                                End If
                                                'Deletes file with -trailer in the filename.
                                                My.Computer.FileSystem.DeleteFile(ParentDir + "\" + strFullFilePath + strFileExtension)
                                                'Moves the file to the parent directory and renames it.
                                                My.Computer.FileSystem.MoveFile(file, ParentDir + "\" + strFullFilePath + strFileExtension)
                                                'Deletes the trailers folder.
                                                My.Computer.FileSystem.DeleteDirectory(strFilePath, FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing)
                                            End If
                                        Else
                                            Dim TrailerCount As Integer
                                            Dim TrailerCheck = My.Computer.FileSystem.GetFiles(ParentDir)

                                            For Each TrailerFile In TrailerCheck
                                                If TrailerFile.ToLower.Contains("-trailer") Then
                                                    TrailerCount = TrailerCount + 1
                                                Else
                                                End If
                                            Next

                                            If TrailerCount > 0 Then
                                                Dim CounterValue

                                                CounterValue = Convert.ToString(TrailerCount)

                                                'Moves the file to the parent directory and renames it.
                                                My.Computer.FileSystem.MoveFile(file, ParentDir + "\" + strFullFilePath + CounterValue + strFileExtension)
                                            Else
                                                'Moves the file to the parent directory and renames it.
                                                My.Computer.FileSystem.MoveFile(file, ParentDir + "\" + strCurrentFile)
                                            End If

                                            If chkDeleteTrailer.Checked = True Then
                                                'Checks if trailers folder is empty
                                                If System.IO.Directory.GetFiles(strFilePath).Count > 0 Then
                                                Else
                                                    My.Computer.FileSystem.DeleteDirectory(strFilePath, FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing)
                                                End If
                                            Else
                                                'Deletes the trailers folder.
                                                My.Computer.FileSystem.DeleteDirectory(strFilePath, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
                                            End If
                                        End If
                                    Else 'If file.ToLower.Contains("-trailer") Then
                                        'Checks to see if a file with -trailer already exists.
                                        If My.Computer.FileSystem.FileExists(ParentDir + "\" + strFullFilePath + strFileExtension) Then
                                            Dim TrailerCount As Integer
                                            Dim TrailerCheck = My.Computer.FileSystem.GetFiles(ParentDir)

                                            For Each TrailerFile In TrailerCheck
                                                If TrailerFile.ToLower.Contains("-trailer") Then
                                                    TrailerCount = TrailerCount + 1
                                                Else
                                                End If
                                            Next

                                            If TrailerCount > 0 Then
                                                Dim CounterValue

                                                CounterValue = Convert.ToString(TrailerCount)

                                                'Moves the file to the parent directory and renames it.
                                                My.Computer.FileSystem.MoveFile(file, ParentDir + "\" + strFullFilePath + "-trailer" + CounterValue + strFileExtension)
                                            Else
                                                'Moves the file to the parent directory and renames it.
                                                My.Computer.FileSystem.MoveFile(file, ParentDir + "\" + strFullFilePath + "-trailer" + strFileExtension)
                                            End If

                                            If chkDeleteTrailer.Checked = True Then
                                                'Checks if trailers folder is empty
                                                If System.IO.Directory.GetFiles(strFilePath).Count > 0 Then
                                                Else
                                                    My.Computer.FileSystem.DeleteDirectory(strFilePath, FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing)
                                                End If
                                            Else
                                                'Deletes the trailers folder.
                                                My.Computer.FileSystem.DeleteDirectory(strFilePath, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
                                            End If
                                            'Checks to see if ReadOnly attribute is set and switches it off.
                                            If GetAttr(ParentDir + "\" + strFullFilePath + strFileExtension) And vbReadOnly Then
                                                Dim attributes As FileAttributes

                                                attributes = GetAttr(ParentDir + "\" + strFullFilePath + strFileExtension)

                                                If (attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
                                                    attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly)
                                                    SetAttr(ParentDir + "\" + strFullFilePath + strFileExtension, attributes)
                                                End If
                                            End If
                                            'Deletes file with -trailer in the filename.
                                            My.Computer.FileSystem.DeleteFile(ParentDir + "\" + strFullFilePath + strFileExtension)
                                        Else
                                            Dim TrailerCount As Integer
                                            Dim TrailerCheck = My.Computer.FileSystem.GetFiles(ParentDir)

                                            For Each TrailerFile In TrailerCheck
                                                If TrailerFile.ToLower.Contains("-trailer") Then
                                                    TrailerCount = TrailerCount + 1
                                                Else
                                                End If
                                            Next

                                            If TrailerCount > 0 Then
                                                Dim CounterValue = Convert.ToString(TrailerCount)
                                                'Moves the file to the parent directory and renames it.
                                                My.Computer.FileSystem.MoveFile(file, ParentDir + "\" + strFullFilePath + "-trailer" + CounterValue + strFileExtension)
                                            Else
                                                'Deletes the trailers folder.
                                                My.Computer.FileSystem.DeleteDirectory(strFilePath, FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing)
                                            End If
                                        End If
                                    End If
                                    If chkDeleteTrailer.Checked = True Then
                                    Else
                                        'Checks if trailers folder is empty
                                        If System.IO.Directory.GetFiles(strFilePath).Count > 0 Then
                                        Else
                                            If My.Computer.FileSystem.DirectoryExists(strFilePath) Then
                                                'Deletes the trailers folder.
                                                My.Computer.FileSystem.DeleteDirectory(strFilePath, FileIO.UIOption.OnlyErrorDialogs, FileIO.UICancelOption.DoNothing)
                                            End If
                                        End If
                                    End If
                                End If
                                'No clue what this does I was just afraid to delete it.  It obviously handles errors somehow.
                            Catch E As Exception
                                Console.WriteLine("Error determining parent directory")
                                Console.WriteLine(E.Message)
                            End Try
                        End If
                        If chkNFO.Checked = True Then
                            If strFileExtension = ".xml" Then
                                Dim xmlDocument As New XmlDocument
                                Dim LocalTitle As XmlNodeList = xmlDocument.GetElementsByTagName("LocalTitle")
                                Dim OriginalTitle As XmlNodeList = xmlDocument.GetElementsByTagName("OriginalTitle")
                                Dim SortTitle As XmlNodeList = xmlDocument.GetElementsByTagName("SortTitle")
                                Dim IMDB As XmlNodeList = xmlDocument.GetElementsByTagName("IMDB")
                                Dim Rating As XmlNodeList = xmlDocument.GetElementsByTagName("Rating")
                                Dim RunningTime As XmlNodeList = xmlDocument.GetElementsByTagName("RunningTime")
                                Dim Description As XmlNodeList = xmlDocument.GetElementsByTagName("Description")
                                Dim Watched As XmlNodeList = xmlDocument.GetElementsByTagName("Watched")
                                Dim LocalTrailer As XmlNodeList = xmlDocument.GetElementsByTagName("LocalTrailer")
                                Dim Genre As XmlNodeList = xmlDocument.GetElementsByTagName("Genre")

                                xmlDocument.Load(strFilePath + "\" + strCurrentFile)

                                Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "movie.nfo", True)
                                    writer.WriteLine("<?xml version=" + Chr(34) + "1.0" + Chr(34) + " encoding=" + Chr(34) + "utf-8" + Chr(34) + "?>")
                                    writer.WriteLine("	<movie>")
                                End Using

                                For Each XMLitem As XmlElement In LocalTitle
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "movie.nfo", True)
                                        writer.WriteLine("		<title>" + XMLitem.InnerText + "</title>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In OriginalTitle
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "movie.nfo", True)
                                        writer.WriteLine("		<originaltitle>" + XMLitem.InnerText + "</originaltitle>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In SortTitle
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "movie.nfo", True)
                                        writer.WriteLine("		<sorttitle>" + XMLitem.InnerText + "</sorttitle>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In Rating
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "movie.nfo", True)
                                        writer.WriteLine("		<rating>" + XMLitem.InnerText + "</rating>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In Description
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "movie.nfo", True)
                                        writer.WriteLine("		<plot>" + XMLitem.InnerText + "</plot>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In IMDB
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "movie.nfo", True)
                                        writer.WriteLine("		<id>" + XMLitem.InnerText + "</id>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In Watched
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "movie.nfo", True)
                                        writer.WriteLine("		<watched>" + XMLitem.InnerText + "</watched>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In RunningTime
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "movie.nfo", True)
                                        writer.WriteLine("		<runtime>" + XMLitem.InnerText + "</runtime>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In LocalTrailer
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "movie.nfo", True)
                                        writer.WriteLine("		<trailer>" + XMLitem.InnerText + "</trailer>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In Genre
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "movie.nfo", True)
                                        writer.WriteLine("		<genre>" + XMLitem.InnerText + "</genre>")
                                    End Using
                                Next

                                Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "movie.nfo", True)
                                    writer.WriteLine("	</movie>")
                                End Using

                                If chkDeleteNFO.Checked = True Then
                                Else
                                    My.Computer.FileSystem.DeleteFile(strFilePath + "\" + strCurrentFile)
                                End If
                            End If
                        End If
                    End If
                End If
            Next
        Next

        pbrBar1.Value = 0

    End Sub

    Private Sub XMBCToMediaBrowser()
        For Each item As String In lstMediaLocations.Items()
            Dim Files As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(item, FileIO.SearchOption.SearchAllSubDirectories)
            Dim FileCollection As New Microsoft.VisualBasic.Collection()

            'Progress Bar sets maximum to the number of files returned in the count of "files."
            With pbrBar1
                .Maximum = Files.Count
                .Minimum = 0
                .Value = 1
            End With

            'Finds each file inside of the text box directory for processing.
            For Each file In Files


                'Advances the progress bar one level for each file processed
                If pbrBar1.Value < pbrBar1.Maximum Then
                    pbrBar1.Value = pbrBar1.Value + 1
                End If

                'Checks to see if the file being processed exists. If not it skips to the next file.
                If My.Computer.FileSystem.FileExists(file) Then
                    If file.Contains("$") Then
                    Else
                        'Checks to see if ReadOnly attribute is set and switches it off.
                        If GetAttr(file) And vbReadOnly Then
                            Dim attributes As FileAttributes

                            attributes = GetAttr(file)

                            If (attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
                                attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly)
                                SetAttr(file, attributes)
                            End If
                        End If

                        'Checks to see if the fanart check box is checked.
                        If chkFanart.Checked = True Then
                            Dim strCurrentFile As String = System.IO.Path.GetFileName(file)
                            Dim strFileExtension As String = System.IO.Path.GetExtension(file)
                            Dim strFilePath As String = System.IO.Path.GetDirectoryName(file)

                            'Checks to make sure file is a picture before renaming.
                            If strFileExtension.ToLower.Contains("jpg") Then
                                'Checks to see if the picture is fanart.
                                If strCurrentFile.ToLower.Contains("fanart") Then
                                    If strFilePath.Contains("extrafanart") Then
                                        If chkDeleteBackdrop.Checked = True Then
                                        Else
                                            If GetAttr(file) And vbReadOnly Then
                                                Dim attributes As FileAttributes

                                                attributes = GetAttr(file)

                                                If (attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
                                                    attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly)
                                                    SetAttr(file, attributes)
                                                End If
                                            End If
                                            My.Computer.FileSystem.DeleteFile(file)
                                        End If
                                    Else
                                        'Checks to see if a file named backdrop already exists.
                                        If My.Computer.FileSystem.FileExists(strFilePath + "\" + "backdrop.jpg") Then
                                            'Checks to see if ReadOnly attribute is set and switches it off.
                                            If GetAttr(strFilePath + "\" + "backdrop.jpg") And vbReadOnly Then
                                                Dim attributes As FileAttributes

                                                attributes = GetAttr(strFilePath + "\" + "backdrop.jpg")

                                                If (attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
                                                    attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly)
                                                    SetAttr(strFilePath + "\" + "backdrop.jpg", attributes)
                                                End If
                                            End If
                                            'Deletes existing backdrop.
                                            My.Computer.FileSystem.DeleteFile(strFilePath + "\" + "backdrop.jpg")
                                            'Renames current file to backdrop.
                                            My.Computer.FileSystem.RenameFile(file, "backdrop" + strFileExtension)
                                        Else
                                            'Renames current file to backdrop.
                                            My.Computer.FileSystem.RenameFile(file, "backdrop" + strFileExtension)
                                        End If
                                    End If
                                End If
                            End If
                        End If

                        'Checks to see if the trailers check box is checked.
                        If chkTrailers.Checked = True Then
                            Dim strFullFilePath As String = My.Computer.FileSystem.GetParentPath(file)
                            Dim strCurrentFile As String = System.IO.Path.GetFileName(file)
                            Dim strFileExtension As String = System.IO.Path.GetExtension(file)
                            Dim strFilePath As String = System.IO.Path.GetDirectoryName(file)

                            'Checks to see if the current file contains "-trailer."
                            If strCurrentFile.ToLower.Contains("-trailer") Then
                                'Checks to see if the trailers folder exists.
                                If My.Computer.FileSystem.DirectoryExists(strFullFilePath + "\" + "trailers") Then
                                    'Checks if a file exists with the same name in the same intended path.
                                    If My.Computer.FileSystem.FileExists(strFilePath + "\" + "trailers" + "\" + strCurrentFile) Then
                                        'Checks to see if ReadOnly attribute is set and switches it off.
                                        If GetAttr(strFilePath + "\" + strCurrentFile) And vbReadOnly Then
                                            Dim attributes As FileAttributes

                                            attributes = GetAttr(strFilePath + "\" + strCurrentFile)

                                            If (attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
                                                attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly)
                                                SetAttr(strFilePath + "\" + strCurrentFile + strCurrentFile, attributes)
                                            End If
                                        End If
                                        'Deletes file in intended path.
                                        My.Computer.FileSystem.DeleteFile(strFilePath + "\" + "trailers" + "\" + strCurrentFile)
                                        'Moves the file into the trailers folder.
                                        System.IO.File.Move(file, strFilePath + "\" + "trailers" + "\" + strCurrentFile)
                                    Else
                                        'Moves file into trailers folder.
                                        System.IO.File.Move(file, strFilePath + "\" + "trailers" + "\" + strCurrentFile)
                                    End If
                                Else
                                    'This is to keep the app from creating nested trailer folders if it's accidentally ran on a library already setup for MediaBrowser.
                                    If strFullFilePath.ToLower.Contains("trailers") Then
                                    Else
                                        'Creates trailers folder
                                        My.Computer.FileSystem.CreateDirectory(strFilePath + "\" + "trailers")
                                        'Moves file into trailers folder.
                                        System.IO.File.Move(file, strFilePath + "\" + "trailers" + "\" + strCurrentFile)
                                    End If
                                End If
                            End If
                        End If
                        If chkNFO.Checked = True Then
                            Dim strFullFilePath As String = My.Computer.FileSystem.GetParentPath(file)
                            Dim strCurrentFile As String = System.IO.Path.GetFileName(file)
                            Dim strFileExtension As String = System.IO.Path.GetExtension(file)
                            Dim strFilePath As String = System.IO.Path.GetDirectoryName(file)
                            Dim strNoExtension As String = System.IO.Path.GetFileNameWithoutExtension(file)

                            If strFileExtension = ".nfo" Then
                                Dim xmlDocument As New XmlDocument
                                Dim LocalTitle As XmlNodeList = xmlDocument.GetElementsByTagName("title")
                                Dim OriginalTitle As XmlNodeList = xmlDocument.GetElementsByTagName("originaltitle")
                                Dim SortTitle As XmlNodeList = xmlDocument.GetElementsByTagName("sorttitle")
                                Dim IMDB As XmlNodeList = xmlDocument.GetElementsByTagName("id")
                                Dim Rating As XmlNodeList = xmlDocument.GetElementsByTagName("rating")
                                Dim RunningTime As XmlNodeList = xmlDocument.GetElementsByTagName("runtime")
                                Dim Description As XmlNodeList = xmlDocument.GetElementsByTagName("plot")
                                Dim Watched As XmlNodeList = xmlDocument.GetElementsByTagName("watched")
                                Dim LocalTrailer As XmlNodeList = xmlDocument.GetElementsByTagName("trailer")
                                Dim Genre As XmlNodeList = xmlDocument.GetElementsByTagName("genre")

                                xmlDocument.Load(strFilePath + "\" + strCurrentFile)

                                Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                    writer.WriteLine("<?xml version=" + Chr(34) + "1.0" + Chr(34) + " encoding=" + Chr(34) + "utf-8" + Chr(34) + " standalone=" + Chr(34) + "yes" + Chr(34) + "?>")
                                    writer.WriteLine("<!--This file is created and copyrighted by My Movies (http://www.mymovies.dk) to store movie meta-data in movie folders, and may not be altered. The data may be used freely for non-commercial purposes only (this means that you may not use it in commercial programs, without written consent). If you would like to use the data for commercial purposes, please contact support@mymovies.dk. The data may not be redistributed, shared, transferred or in any way used in ways that cannot be referred to as personal usage by the original user. This means that using the data on websites, services or similar is strictly forbidden.-->")
                                    writer.WriteLine("<Title>")
                                End Using

                                Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                    writer.WriteLine("  <ID>" + "</ID>")
                                    writer.WriteLine("  <WebServiceID>" + "</WebServiceID>")
                                    writer.WriteLine("  <CollectionNumber>" + "</CollectionNumber>")
                                    writer.WriteLine("  <Type>" + "</Type>")
                                    writer.WriteLine("  <Barcode>" + "</Barcode>")
                                    writer.WriteLine("  <Country>" + "</Country>")
                                End Using

                                For Each XMLitem As XmlElement In LocalTitle
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                        writer.WriteLine("  <LocalTitle>" + XMLitem.InnerText + "</LocalTitle>")
                                        writer.WriteLine("  <LocalTitleDisplay>" + XMLitem.InnerText + "</LocalTitleDisplay>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In OriginalTitle
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                        writer.WriteLine("  <OriginalTitle>" + XMLitem.InnerText + "</OriginalTitle>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In SortTitle
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                        writer.WriteLine("  <SortTitle>" + XMLitem.InnerText + "</SortTitle>")
                                        writer.WriteLine("  <SortTitleDisplay>" + XMLitem.InnerText + "</SortTitleDisplay>")
                                    End Using
                                Next

                                Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                    writer.WriteLine("  <Edition>")
                                    writer.WriteLine("  </Edition>")
                                End Using

                                For Each XMLitem As XmlElement In IMDB
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                        writer.WriteLine("  <IMDB>" + XMLitem.InnerText + "</IMDB>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In Rating
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                        writer.WriteLine("  <Rating>" + XMLitem.InnerText + "</Rating>")
                                    End Using
                                Next

                                Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                    writer.WriteLine("  <AspectRatio>" + "</AspectRatio>")
                                    writer.WriteLine("  <VideoStandard>" + "</VideoStandard>")
                                    writer.WriteLine("  <ProductionYear>" + "</ProductionYear>")
                                    writer.WriteLine("  <ReleaseDate>" + "</ReleaseDate>")
                                End Using

                                For Each XMLitem As XmlElement In RunningTime
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                        writer.WriteLine("  <RunningTime>" + XMLitem.InnerText + "</RunningTime>")
                                    End Using
                                Next

                                Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                    writer.WriteLine("  <TagLine />")
                                End Using

                                For Each XMLitem As XmlElement In Description
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                        writer.WriteLine("  <Description><! [CDATA[" + XMLitem.InnerText + "]]</Description>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In Watched
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                        writer.WriteLine("  <Watched>" + XMLitem.InnerText + "</Watched>")
                                    End Using
                                Next
                                For Each XMLitem As XmlElement In LocalTrailer
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                        writer.WriteLine("  <LocalTrailer>" + XMLitem.InnerText + "  </LocalTrailer>")
                                    End Using
                                Next

                                Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                    writer.WriteLine("  <Genre>")
                                End Using

                                For Each XMLitem As XmlElement In Genre
                                    Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                        writer.WriteLine("    <Genre>" + XMLitem.InnerText + "</Genre>")
                                    End Using
                                Next

                                Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                    writer.WriteLine("  </Genre>")
                                End Using

                                Using writer As StreamWriter = New StreamWriter(strFilePath + "\" + "mymovies.xml", True)
                                    writer.WriteLine("</Title>")
                                End Using

                                If chkDeleteNFO.Checked = True Then
                                Else
                                    My.Computer.FileSystem.DeleteFile(strFilePath + "\" + strCurrentFile)
                                End If
                            End If
                        End If
                    End If
                End If
            Next
        Next

        pbrBar1.Value = 0

    End Sub
End Class

